import Vue from 'vue'
import axios from 'axios'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import store from './store'
import {
  getRequest,
  postRequest
} from './util/requests.js'
import router from './router'
Vue.prototype.$store = store
Vue.prototype.$axios = axios
Vue.prototype.$getRequest = getRequest
Vue.prototype.$postRequest = postRequest
Vue.use(ElementUI)

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (store.state.user.username) { //store.state.user.username
      next()
    } else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      })
    }
  } else {
    next()
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  render: h => h(App),
  router,
  store,
  components: { App },
  template: '<App/>'
})
